/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Mansur
 */
@Entity
@Table(name = "ref_sv_match")
@NamedQueries({
    @NamedQuery(name = "RefShipVisitMatch.findAll", query = "SELECT s FROM RefShipVisitMatch s")})
public class RefShipVisitMatch implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ref_sm_id")
    private Integer refShipVisitMatchId;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ref_sh_id")
    private RefShipVisitHeader shipVisitHeader;
    
    @Embedded
    private InfoAudit infoAudit;
    
    @OneToMany (mappedBy = "shipVisitMatch" , fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    private List<RefShipVisitMatchSub> listMatchSub;
    
    @OneToMany (mappedBy = "shipVisitMatch" , fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    private List<RefShipVisitMatchSub> listAllMatchSub;
    
    public RefShipVisitMatch(){
        
    }

    public Integer getRefShipVisitMatchId() {
        return refShipVisitMatchId;
    }

    public void setRefShipVisitMatchId(Integer refShipVisitMatchId) {
        this.refShipVisitMatchId = refShipVisitMatchId;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public RefShipVisitHeader getShipVisitHeader() {
        return shipVisitHeader;
    }

    public void setShipVisitHeader(RefShipVisitHeader shipVisitHeader) {
        this.shipVisitHeader = shipVisitHeader;
    }

    public InfoAudit getInfoAudit() {
        return infoAudit;
    }

    public void setInfoAudit(InfoAudit infoAudit) {
        this.infoAudit = infoAudit;
    }

    public List<RefShipVisitMatchSub> getListMatchSub() {
        List<RefShipVisitMatchSub> subs = new ArrayList<RefShipVisitMatchSub>();
        for (RefShipVisitMatchSub s : listMatchSub) {
            if(s.getShipVisitSub().getActiveFlag().equals("A")){
                subs.add(s);
            }
        }
        listMatchSub.clear();
        listMatchSub = subs;
        
        //sorting        
        Collections.sort(listMatchSub, new Comparator<RefShipVisitMatchSub>(){
            @Override
            public int compare(RefShipVisitMatchSub sub1, RefShipVisitMatchSub sub2){
                return sub1.getShipVisitSub().getOrderFlag().compareTo(sub2.getShipVisitSub().getOrderFlag());
            }
        });
        return listMatchSub;
    }

    public void setListMatchSub(List<RefShipVisitMatchSub> listMatchSub) {
        this.listMatchSub = listMatchSub;
    }

    public List<RefShipVisitMatchSub> getListAllMatchSub() {
        Collections.sort(listAllMatchSub, new Comparator<RefShipVisitMatchSub>(){
            @Override
            public int compare(RefShipVisitMatchSub sub1, RefShipVisitMatchSub sub2){
                return sub1.getShipVisitSub().getOrderFlag().compareTo(sub2.getShipVisitSub().getOrderFlag());
            }
        });
        return listAllMatchSub;
    }

    public void setListAllMatchSub(List<RefShipVisitMatchSub> listAllMatchSub) {
        this.listAllMatchSub = listAllMatchSub;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (refShipVisitMatchId != null ? refShipVisitMatchId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof RefShipVisitMatch)) {
            return false;
        }
        RefShipVisitMatch other = (RefShipVisitMatch) object;
        if ((this.refShipVisitMatchId == null && other.refShipVisitMatchId != null) || (this.refShipVisitMatchId != null && !this.refShipVisitMatchId.equals(other.refShipVisitMatchId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "exatrez.model.RefShipVisitMatch[ refShipVisitMatchId=" + refShipVisitMatchId + " ]";
    } 
    
}
